package com.szt.sy.util.excel;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.servlet.view.document.AbstractXlsView;

import com.szt.sy.util.date.DateUtil;

/***
 * 导入到EXCEL
 * 
 * @author wwy
 *
 */
public class ObjectExcelView extends AbstractXlsView {
	
	@Override
	protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		// TODO Auto-generated method stub
		Date date = new Date();
		String filename = DateUtil.getSdfTimes(date);
		Sheet sheet;
		Cell cell;
		response.setContentType("application/octet-stream");
		response.setHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
		sheet = workbook.createSheet("sheet1");
		
		List<String> titles = (List<String>) model.get("titles");
		int len = titles.size();
		// 标题样式
		CellStyle headerStyle = workbook.createCellStyle();
		headerStyle.setAlignment(HorizontalAlignment.CENTER);
		headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		// 标题字体
		Font headerFont = workbook.createFont();
		headerFont.setBold(true);
		headerFont.setFontHeightInPoints((short) 11);
		headerStyle.setFont(headerFont);
		short width = 20, height = 25 * 20;
		sheet.setDefaultColumnWidth(width);
		// 设置标题
		for (int i = 0; i < len; i++) {
			String title = titles.get(i);
			cell = getCell(sheet, 0, i);
			
			cell.setCellStyle(headerStyle);
			cell.setCellType(CellType.STRING);
			cell.setCellValue(title);
		}
		sheet.getRow(0).setHeight(height);
		// 内容样式
		CellStyle contentStyle = workbook.createCellStyle();
		contentStyle.setAlignment(HorizontalAlignment.CENTER);
		List<Map<String, Object>> varList = (List<Map<String, Object>>) model.get("varList");
		int varCount = varList.size();
		for (int i = 0; i < varCount; i++) {
			Map<String, Object> vpd = varList.get(i);
			for (int j = 0; j < len; j++) {
				String varstr = vpd.get("var" + (j + 1)) != null ? vpd.get("var" + (j + 1)).toString() : "";
				cell = getCell(sheet, i + 1, j);
				cell.setCellStyle(contentStyle);
				cell.setCellType(CellType.STRING);
				cell.setCellValue(varstr);
			}
			
		}
	}
	
	private Cell getCell(Sheet sheet, int row, int col) {
		Row sheetRow = sheet.getRow(row);
		if (sheetRow == null) {
			sheetRow = sheet.createRow(row);
		}
		Cell cell = sheetRow.getCell(col);
		if (cell == null) {
			cell = sheetRow.createCell(col);
		}
		return cell;
	}
	
}
